﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;
using System.Threading;

namespace BSTService
{
    partial class JWBSTService : ServiceBase
    {
        string path = "";
        static int width = 300;
        static int height = 200;
        static int timespan = 60000;
        static string FilterType = "*.jpg|*.png|*.gif|*.bmp";
        #region 文件监听
        static FileSystemWatcher watcher = new FileSystemWatcher();

        /// <summary>
        /// 初始化监听
        /// </summary>
        /// <param name="StrWarcherPath">需要监听的目录</param>
        /// <param name="FilterType">需要监听的文件类型(筛选器字符串)</param>
        /// <param name="IsEnableRaising">是否启用监听</param>
        /// <param name="IsInclude">是否监听子目录</param>
        private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
        {
            //初始化监听
            //watcher.BeginInit();
            //设置监听文件类型
            //watcher.Filter = FilterType;
            //设置是否监听子目录
            watcher.IncludeSubdirectories = IsInclude;
            
            //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
            //watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; // 只需要监听文件名
            
            //设置监听的路径
            watcher.Path = StrWarcherPath;
            //注册创建文件或目录时的监听事件
            watcher.Created += new FileSystemEventHandler(watch_created);
            //注册当指定目录的文件或者目录发生改变的时候的监听事件
            watcher.Changed += new FileSystemEventHandler(watch_changed);
            //注册当删除目录的文件或者目录的时候的监听事件
            watcher.Deleted += new FileSystemEventHandler(watch_deleted);
            //当指定目录的文件或者目录发生重命名的时候的监听事件
            watcher.Renamed += new RenamedEventHandler(watch_renamed);

            //设置是否启用监听?
            watcher.EnableRaisingEvents = IsEnableRaising;
            //结束初始化
            //watcher.EndInit();
            
        }
        /// <summary>
        /// 创建文件或者目录时的监听事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_created(object sender, FileSystemEventArgs e)
        {
            // 如果是创建缩略图，则不处理
            if (e.Name.Contains("_min"))
                return;
            string ext = Path.GetExtension(e.Name);
            // 如果文件类型为图片类型，缩放图片
            if (FilterType.Contains(ext.ToLower()))
            {
                // 缩放文件
                Tools.CreateThumNailFile(e.FullPath, width, height, ResizeType.W);
            }
        }
        /// <summary>
        /// 当指定目录的文件或者目录发生改变的时候的监听事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_changed(object sender, FileSystemEventArgs e)
        {
            //事件内容
        }
        /// <summary>
        /// 当删除目录的文件或者目录的时候的监听事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_deleted(object sender, FileSystemEventArgs e)
        {
            //事件内容
        }
        /// <summary>
        /// 当指定目录的文件或者目录发生重命名的时候的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void watch_renamed(object sender, RenamedEventArgs e)
        {
            //事件内容
        }
        /// <summary>
        /// 启动或者停止监听
        /// </summary>
        /// <param name="IsEnableRaising">True:启用监听,False:关闭监听</param>
        private void WatchStartOrSopt(bool IsEnableRaising)
        {
            watcher.EnableRaisingEvents = IsEnableRaising;
        }

        #endregion

        public JWBSTService()
        {
            InitializeComponent();
        }
        
        protected override void OnStart(string[] args)
        {
            try
            {
                // TODO: 在此处添加代码以启动服务。
                path = ConfigurationManager.AppSettings["ImagePath"];
                width = int.Parse(ConfigurationManager.AppSettings["width"]);
                height = int.Parse(ConfigurationManager.AppSettings["height"]);
                FilterType = ConfigurationManager.AppSettings["FilterType"];

                
                WatcherStrat(path, FilterType, true, true);
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            
            WatchStartOrSopt(false);
        }

        private static void WriteLog(string log)
        {
            StreamWriter sw = new StreamWriter("D:\\txt.log",true);
            sw.WriteLine(log);
            sw.Close();
        }
    }
}
